package com.qiankan.qmsj.ushare;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.qiankan.qmsj.R;
import com.qiankan.qmsj.activity.BaseActivity;
import com.qiankan.qmsj.widget.TipToast;

/**
 * 自定义分享页面
 */
public class MyShareActivity extends BaseActivity implements View.OnClickListener {

    private String shareUrl, title, content, sharePicUrl;//链接,标题，内容，图标

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_share;
    }

    public static Intent newIntent(Context context, Bundle bundle) {
        Intent intent = new Intent(context, MyShareActivity.class);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout wechat = (RelativeLayout) findViewById(R.id.rl_share_wechat);
        RelativeLayout wechat_circle = (RelativeLayout) findViewById(R.id.rl_share_wechat_circle);
        RelativeLayout qq = (RelativeLayout) findViewById(R.id.rl_share_qq);
        RelativeLayout qqzone = (RelativeLayout) findViewById(R.id.rl_share_qqzone);
        RelativeLayout sina = (RelativeLayout) findViewById(R.id.rl_share_sina);
        RelativeLayout others = (RelativeLayout) findViewById(R.id.rl_others);
        TextView tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        wechat.setOnClickListener(this);
        wechat_circle.setOnClickListener(this);
        qq.setOnClickListener(this);
        qqzone.setOnClickListener(this);
        sina.setOnClickListener(this);
        others.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            shareUrl = bundle.getString("shareUrl", "");
            title = bundle.getString("title", "荣耀钱包");
            content = "缺钱花，来荣耀钱包";
//            content = bundle.getString("content", "缺钱花，来借贷钱包");
            sharePicUrl = bundle.getString("sharePicUrl", "");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_share_wechat:
                share(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.rl_share_wechat_circle:
                share(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.rl_share_qq:
                share(SHARE_MEDIA.QQ);
                break;
            case R.id.rl_share_qqzone:
                share(SHARE_MEDIA.QZONE);
                break;
            case R.id.rl_share_sina:
                share(SHARE_MEDIA.SINA);
                break;
            case R.id.rl_others:
            case R.id.tv_cancle:
                overridePendingTransition(0, R.anim.out);
                finish();
                break;
        }
    }

    private void share(SHARE_MEDIA platform) {
        //开启自定义分享页面
        UMImage image;
        /*if (TextUtils.isEmpty(sharePicUrl)) {
            image = new UMImage(this, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        } else {
            image = new UMImage(this, sharePicUrl);//网络图片
        }*/
        image = new UMImage(this, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        new ShareAction(this)
                .setPlatform(platform)
                .setCallback(umShareListener)
                .withTitle(title)
                .withText(content)
                .withTargetUrl(shareUrl)
                .withMedia(image)
                .share();

        /*new ShareAction(this)
                .setPlatform(SHARE_MEDIA.QQ)//传入平台
                .withText("hello")//分享内容
                .setCallback(umShareListener)//回调监听器
                .share();*/
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            TipToast.toast(MyShareActivity.this, "分享成功啦");
            finish();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            TipToast.toast(MyShareActivity.this, "分享失败");
            finish();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            TipToast.toast(MyShareActivity.this, "分享取消了");
            finish();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
